package com.oim.ui.fx.common.chat;

import org.controlsfx.control.GridCell;
import org.controlsfx.control.GridView;

import java.util.HashMap;
import java.util.Map;

import javafx.event.EventHandler;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.StackPane;
import javafx.util.Callback;

/**
 * @author 李玉刚
 */
public class ChatFilePane extends StackPane {
    private GridView<String> gridView;
    private Map<String, String> fileMap = new HashMap<>();

    public ChatFilePane() {

        initGridView();
    }

    private void initGridView() {
        gridView = new GridView<>();
        getChildren().add(gridView);
        gridView.setOnScrollFinished(event -> {
            System.out.println("到底了");
        });
        gridView.setHorizontalCellSpacing(10);
        gridView.setVerticalCellSpacing(10);
        gridView.setCellWidth(100);
        gridView.setCellFactory(new Callback<GridView<String>, GridCell<String>>() {
            @Override
            public GridCell<String> call(GridView<String> param) {
                return new GridCell<String>(){
                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if(empty){
                            setGraphic(null);
                        }else{
                            setGraphic(new ImageView(new Image(fileMap.get(item),100,0,true,true)));
                        }
                    }
                };
            }
        });

    }

    public void addImage(String id, String src) {
        fileMap.put(id, src);
        if(!gridView.getItems().contains(id)) {
            gridView.getItems().add(id);
        }
    }
}
